package erp.education.server.beans.implementation.setup.school;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import erp.education.client.setup.school.CoursesRequest;
import erp.education.server.beans.implementation.BaseService;
import erp.education.server.beans.implementation.ResultResponse;
import erp.education.server.beans.implementation.ServiceObject;
import erp.education.server.beans.interfaces.setup.school.CoursesServiceI;
import erp.education.server.beans.interfaces.user.transaction.ServiceObjectI;
import erp.education.server.db.entites.setup.school.Courses;
import erp.education.server.db.entites.setup.school.Schools;
import erp.education.server.db.exceptions.DbExceptions;
@Component("coursesService")
public class CoursesService extends BaseService implements CoursesServiceI {

	@Override
	public ServiceObjectI coursesLoadService(CoursesRequest coursesRequest) throws DbExceptions {
		ServiceObjectI response=new ServiceObject();
		ResultResponse holder=new ResultResponse();
		 List result=new ArrayList();
		result.add(coursesDao.getCourses());
		holder.setResultList(result);
		response.setResponseObject(holder); 
		return response;
	}


	@Override
	public ServiceObjectI saveUpdateCoursesDetails(CoursesRequest coursesRequest) throws DbExceptions {
		ServiceObjectI response=new ServiceObject();
		ResultResponse holder=new ResultResponse();
		 List result=new ArrayList();
		 Courses course=null;
		 course=coursesDao.getCourse(coursesRequest.getCourseId());
		 if(null==course){
			 course=coursesDao.getCourse(coursesRequest.getCourseName());
			if(null==course){
			 course=new Courses();
			 course.setCourseName(coursesRequest.getCourseName());
			 coursesDao.merge(course);
			}
		 }else{
			 course=coursesDao.getCourse(coursesRequest.getCourseName());
			 if(null==course){
				 course=coursesDao.getCourse(coursesRequest.getCourseId());
				 course.setCourseName(coursesRequest.getCourseName());
				 coursesDao.merge(course);
			 }
		 }
		 
		 
		  
		
		result.add(coursesDao.getCourses());
		holder.setResultList(result);
		response.setResponseObject(holder); 
		return response;
	}
}
